﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Data;

using SubSonic.Schema;

namespace MvcForum.Net.Business
{
    public class UsersRepository
    {


        public static List<UsersAction> UserActionList(int pageIndex, int pageSize, out int totalRows)
        {

            var _sp = new MainDB().UsersActionsList(pageIndex, pageSize);

            var _list = _sp.ExecuteTypedList<UsersAction>();

            totalRows = Convert.ToInt32(_sp.OutputParameters.GetParameter("TotalRows").ParameterValue);
            return _list;


        }


        public static List<UserEntity> UsersList(int pageIndex, int pageSize, out int totalRows)
        {
            var _sp = new MainDB().UsersList(pageIndex, pageSize);

            var _list = _sp.ExecuteTypedList<UserEntity>();

            totalRows = Convert.ToInt32(_sp.OutputParameters.GetParameter("TotalRows").ParameterValue);
            return _list;
            
        }


        public static List<UserOnline> UserOnlineList(int pastMinutes)
        {
            return new MainDB().UsersOnline(pastMinutes).ExecuteTypedList<UserOnline>();
        }

        public static void UserActivityUpdate(string sesionId, int userId, string userName, string ip, string pageTitle, string location, int forumId, int topicId, string browser, string platform, int min)
        {
            new MainDB().UsersActionsSave(sesionId, userId, userName, ip, DateTime.UtcNow, pageTitle, location, forumId,
                                          topicId, browser, platform, min).Execute();
        }


        public static UserEntity UserInfo(int userId)
        {
            var _list = new MainDB().UserInfo(userId, null).ExecuteTypedList<UserEntity>();
            return _list.Count > 0 ? _list[0] : null;
        }

        public static UserEntity UserInfo(string userName)
        {
            var _list = new MainDB().UserInfo(0, userName).ExecuteTypedList<UserEntity>();
            return _list.Count > 0 ? _list[0] : null;
        }


        public static void UpdateActiveCode(string email,string activeCode)
        {
           new MainDB().UsersUpdateActiveCode(email,activeCode).Execute();
            
        }

        public static void ResetPwd(int userId, string newPwd)
        {
            new MainDB().UsersResetPwd(userId, newPwd).Execute();

        }



        public static User UserInfo(string activeCode,string email)
        {
            var _list = new MainDB().UsersActive(activeCode,email).ExecuteTypedList<User>();
            return _list.Count > 0 ? _list[0] : null;
        }


        public static UserInfo LogIn(string username, string password, bool updateState)
        {
            var _mainDB = new MainDB();
            UserInfo _userInfo = null;


            using (IDataReader _dr = _mainDB.LogIn(username, password, updateState).ExecuteReader())
            {
                if (_dr.Read())
                {
                    _userInfo = new UserInfo();
                    _userInfo.UserId = Convert.ToInt32(_dr["UserId"]);
                    _userInfo.Email = _dr["Email"].ToString();
                    _userInfo.UserName = _dr["UserName"].ToString();
                    _userInfo.RoleId = Convert.ToInt32(_dr["RoleId"]);
                    _userInfo.IsActived = Convert.ToBoolean(_dr["IsActived"]);
                    bool _isAdmin = Convert.ToBoolean(_dr["IsAdmin"]);
                    _userInfo.IsAdmin = _isAdmin;
                    _userInfo.IsModerator = _isAdmin ? true : false;
                    _userInfo.TimeZone = Convert.ToSingle(_dr["TimeZone"]);
                    if (!_isAdmin)
                    {

                        var _am = new AccessMask();
                        _am.LoadFromString(_dr["AccessMask"].ToString());

                        _userInfo.Permissions = _am;
                    }
                }


            }

            if (_userInfo != null && !_userInfo.IsAdmin)
            {
                using (IDataReader _dr = _mainDB.ModeratorList(_userInfo.UserId).ExecuteReader())
                {

                    var _forums = new ArrayList();

                    while (_dr.Read())
                    {

                        if (!_forums.Contains(_dr["ForumId"]))
                            _forums.Add(_dr["ForumId"]);

                    }

                    _userInfo.IsModerator = _forums.Count > 0;

                    _userInfo.Moderates = _forums;

                }


            }



            return _userInfo;

        }


        public static bool UserNameExists(string userName)
        {

            StoredProcedure _sp = new MainDB().UserNameExists(userName);
            _sp.Execute();
            return Convert.ToBoolean(_sp.OutputParameters.GetParameter("RETURN_VALUE").ParameterValue);
        }

        public static bool ChangePwd(int userId, string oldPwd, string pwd)
        {

            StoredProcedure _sp = new MainDB().ChangePassword(userId, oldPwd, pwd);
            _sp.Execute();
            return Convert.ToBoolean(_sp.OutputParameters.GetParameter("RETURN_VALUE").ParameterValue);
        }


        public static bool EmailExists(string email)
        {

            StoredProcedure _sp = new MainDB().EmailExists(email);
            _sp.Execute();
            return Convert.ToBoolean(_sp.OutputParameters.GetParameter("RETURN_VALUE").ParameterValue);
            //return Convert.ToBoolean(_sp.Output[0]);
        }



        public static void Register(string username, string pwd, string email, double timeZone, string activecode)
        {

            new MainDB().Register(username, pwd, email, timeZone, activecode).Execute();
        }


    }
}
